package com.jp.knowledge.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.TeamCircleConstactsModel;
import com.jp.knowledge.my.model.SearchResultModel;

import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.List;

public class TeamCircleSelectVisibilityActivity extends SlidingActivity implements View.OnClickListener {

    public static final String VISIBILITY_TYPE = "visibilityType";
    public static final String TEAM_CIRCLE_CONSTACTS_MODEL_LIST = "teamCircleConstactsModelList";
    public static final String TEAM_CIRCLE_COMPANY_MODEL_LIST = "teamCircleCompanyModelList";
    public static final String HIDE_COMPANY_SELETC = "hideCompanySelect";

    protected static final int SET_VISIBILITY_CODE = 0x01;
    protected static final int SET_VISIBILITY_COMPANY_CODE = 0x02;

    @ViewInject(R.id.team_circle_visibility_all)
    protected LinearLayout visibilityAll;
    protected ImageView visibilityAllLeftIcon;
    protected TextView visibilityAllLeftText;
    protected TextView visibilityAllRightText;
    @ViewInject(R.id.team_circle_visibility_someone)
    protected LinearLayout visibilitySomeone;
    protected ImageView visibilitySomeoneLeftIcon;
    protected TextView visibilitySomeoneLeftText;
    protected TextView visibilitySomeoneRightText;
    @ViewInject(R.id.team_circle_visibility_others)
    protected LinearLayout visibilityOthers;
    protected ImageView visibilityOthersLeftIcon;
    protected TextView visibilityOthersLeftText;
    protected TextView visibilityOthersRightText;

    protected Intent intent;
    protected int visibilityType;
    protected List<TeamCircleConstactsModel> visibilityModelList;
    protected List<SearchResultModel> visibilityCompanyModeList;

    @Override
    protected int getResourceId() {
        return R.layout.team_circle_item_select_visibility_type;
    }

    @Override
    protected void init() {
        initTitle();

        visibilityAllLeftIcon = (ImageView) visibilityAll.findViewById(R.id.left_icon);
        visibilityAllLeftText = (TextView) visibilityAll.findViewById(R.id.left_text);
        visibilityAllRightText = (TextView) visibilityAll.findViewById(R.id.right_text);
        visibilitySomeoneLeftIcon = (ImageView) visibilitySomeone.findViewById(R.id.left_icon);
        visibilitySomeoneLeftText = (TextView) visibilitySomeone.findViewById(R.id.left_text);
        visibilitySomeoneRightText = (TextView) visibilitySomeone.findViewById(R.id.right_text);
        visibilityOthersLeftIcon = (ImageView) visibilityOthers.findViewById(R.id.left_icon);
        visibilityOthersLeftText = (TextView) visibilityOthers.findViewById(R.id.left_text);
        visibilityOthersRightText = (TextView) visibilityOthers.findViewById(R.id.right_text);
        visibilityAllLeftText.setText("全公司");
        visibilitySomeoneLeftText.setText("部分可看");
        visibilityOthersLeftText.setText("其他公司");
        visibilityAllRightText.setText("");
        visibilitySomeoneRightText.setText("");
        visibilityOthersRightText.setText("");
        visibilityAllLeftIcon.setImageResource(0);
        visibilitySomeoneLeftIcon.setImageResource(0);
        visibilityOthersLeftIcon.setImageResource(0);

        findViewById(R.id.team_circle_visibility_all).setOnClickListener(this);
        findViewById(R.id.team_circle_visibility_someone).setOnClickListener(this);
        findViewById(R.id.team_circle_visibility_others).setOnClickListener(this);

        intent = getIntent();
//        visibilityType = intent.getIntExtra(VISIBILITY_TYPE, 1);
        setTitleLeftIcon(intent.getIntExtra(VISIBILITY_TYPE, 1));
        visibilityModelList = (List<TeamCircleConstactsModel>)
                intent.getSerializableExtra(TEAM_CIRCLE_CONSTACTS_MODEL_LIST);
        visibilityCompanyModeList = (List<SearchResultModel>)
                intent.getSerializableExtra(TEAM_CIRCLE_COMPANY_MODEL_LIST);

        if(intent.getBooleanExtra(HIDE_COMPANY_SELETC, false))
            visibilityOthers.setVisibility(View.GONE);

        if(visibilityType == 2){
            String result = "";
            for(int i = 0; i < visibilityModelList.size(); i++){
                result += visibilityModelList.get(i).getUserName();
                if(i < visibilityModelList.size()-1)
                    result += "、";
            }
            if(visibilityType == 3) {
                visibilityOthersRightText.setText("");
                visibilityCompanyModeList.clear();
            }
            visibilitySomeoneRightText.setText(result);
        } else if(visibilityType == 3){
            String result = "";
            for(int i = 0; i < visibilityCompanyModeList.size(); i++){
                result += visibilityCompanyModeList.get(i).getName();
                if(i < visibilityCompanyModeList.size()-1)
                    result += "、";
            }
            if(visibilityType == 2) {
                visibilitySomeoneRightText.setText("");
                visibilitySomeoneRightText.clearComposingText();
            }
            visibilityOthersRightText.setText(result);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_OK || data == null)
            return;
        if(requestCode == SET_VISIBILITY_CODE){
            visibilityModelList = (List<TeamCircleConstactsModel>)
                    data.getSerializableExtra(TeamCircleConstactsActivity.SELECT_CONSTACTS_DATA);
            String result = "";
            for(int i = 0; i < visibilityModelList.size(); i++){
                result += visibilityModelList.get(i).getUserName();
                if(i < visibilityModelList.size()-1)
                    result += "、";
            }
            if(visibilityType == 3) {
                visibilityOthersRightText.setText("");
                visibilityCompanyModeList.clear();
            }
            setTitleLeftIcon(2);
            visibilitySomeoneRightText.setText(result);
        } else if(requestCode == SET_VISIBILITY_COMPANY_CODE){
            visibilityCompanyModeList = (List<SearchResultModel>)
                    data.getSerializableExtra(TeamCircleSearchCompanyActivity.SELECT_COMPANY_DATA);
            String result = "";
            for(int i = 0; i < visibilityCompanyModeList.size(); i++){
                result += visibilityCompanyModeList.get(i).getName();
                if(i < visibilityCompanyModeList.size()-1)
                    result += "、";
            }
            if(visibilityType == 2) {
                visibilitySomeoneRightText.setText("");
                visibilitySomeoneRightText.clearComposingText();
            }
            setTitleLeftIcon(3);
            visibilityOthersRightText.setText(result);
        }
    }

    private void initTitle(){
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        topName.setText("谁可以看");
        topDesc.setVisibility(View.GONE);
        rightIcon.setVisibility(View.GONE);
        rightBtn.setText("完成");
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setOnClickListener(this);
    }

    protected void setTitleLeftIcon(int position){
        if(visibilityType == position)
            return;
        if(visibilityType == 1){
            visibilityAllLeftIcon.setImageResource(0);
        } else if(visibilityType == 2){
            visibilitySomeoneLeftIcon.setImageResource(0);
        } else if(visibilityType == 3){
            visibilityOthersLeftIcon.setImageResource(0);
        }
        if(position == 1){
            visibilityAllLeftIcon.setImageResource(R.mipmap.choice);
        } else if(position == 2){
            visibilitySomeoneLeftIcon.setImageResource(R.mipmap.choice);
        } else if(position == 3){
            visibilityOthersLeftIcon.setImageResource(R.mipmap.choice);
        }
        visibilityType = position;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.right_bn:
                Intent finishIntent = getIntent();
                finishIntent.putExtra(VISIBILITY_TYPE, visibilityType);
                finishIntent.putExtra(TEAM_CIRCLE_CONSTACTS_MODEL_LIST, (Serializable) visibilityModelList);
                finishIntent.putExtra(TEAM_CIRCLE_COMPANY_MODEL_LIST, (Serializable) visibilityCompanyModeList);
                setResult(RESULT_OK, finishIntent);
                finish();
                break;
            case R.id.team_circle_visibility_all:
                visibilityModelList.clear();
                setTitleLeftIcon(1);
                visibilitySomeoneRightText.setText("");
                break;
            case R.id.team_circle_visibility_someone:
                Intent someoneIntent = new Intent(this, TeamCircleConstactsActivity.class);
                someoneIntent.putExtra(TeamCircleConstactsActivity.TITLE, "部分可见");
                someoneIntent.putExtra(TeamCircleConstactsActivity.SELECT_CONSTACTS_DATA,
                        (Serializable) visibilityModelList);
                startActivityForResult(someoneIntent, SET_VISIBILITY_CODE);
                break;
            case R.id.team_circle_visibility_others:
//                Intent othersIntent = new Intent(this, TeamCircleSearchCompanyActivity.class);
                Intent othersIntent = new Intent(this, TeamCirclePushToCompanyActivity.class);
                othersIntent.putExtra(TeamCircleSearchCompanyActivity.SELECT_COMPANY_DATA,
                        (Serializable) visibilityCompanyModeList);
                startActivityForResult(othersIntent, SET_VISIBILITY_COMPANY_CODE);
                break;
            default:
                return;
        }
    }
}
